package cn.com.taiji.oospub.wechat.controller;

import cn.com.taiji.oospub.wechat.entity.Accesstokensave;
import cn.com.taiji.oospub.wechat.service.IAccesstokensaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/")
public class IndexController {

    @Value("${index_url}")
    private String indexUrl;
    @Autowired
    private IAccesstokensaveService accesstokensaveService;

    @GetMapping("/index/{address}")
    public void index(@PathVariable("address") String address, HttpServletResponse response) throws IOException {
        response.sendRedirect(indexUrl.replaceAll("SPACECODE", address.substring(1, address.length() - 1)));
    }


    @GetMapping("/setAccesstoken")
    @ResponseBody
    public void setAccesstoken(@RequestParam("token") String token) {
        Accesstokensave accesstokensave = accesstokensaveService.query().one();
        accesstokensave.setAccessToken(token);
        accesstokensaveService.updateById(accesstokensave);
    }

}
